{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ML Based Validators\n",
    "\n",
    "Although simple validation can often be handled with rule-based approaches, more complex scenarios require machine learning models for effective validation. \n",
    "\n",
    "ML based validators are able to handle more complex scenarios, providing some level of 'intelligence' to the validation method that is used."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the Guardrails Hub, we provide an easy way to filter and search for different validator types!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Validator Hub Filtering](img/infra_filter.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While different validators may be rules-based or machine learning based, it is still just as easy to implement both in your codebase."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guardrails import Guard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "source": [
    "```bash\n",
    "guardrails hub install hub://guardrails/competitor_check\n",
    "guardrails hub install hub://guardrails/regex_match\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rules-based validators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guardrails.hub import RegexMatch\n",
    "\n",
    "guard = Guard().use(RegexMatch, regex=r\"^[a-zA-Z0-9_]+$\")\n",
    "\n",
    "result = guard(\n",
    "    model=\"gpt-3.5-turbo\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
    "        {\"role\": \"user\", \"content\": \"Tell me about the Apple Iphone.\"},\n",
    "    ],\n",
    "    max_tokens=1024,\n",
    "    temperature=0,\n",
    ")\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ML Based Validator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guardrails.hub import CompetitorCheck\n",
    "\n",
    "guard = Guard().use(CompetitorCheck, [\"Apple\"])\n",
    "\n",
    "result = guard(\n",
    "    model=\"gpt-3.5-turbo\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
    "        {\"role\": \"user\", \"content\": \"Tell me about the Apple Iphone.\"},\n",
    "    ],\n",
    "    max_tokens=1024,\n",
    "    temperature=0,\n",
    ")\n",
    "print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
